package com.mobile.proj;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.repackaged.org.json.JSONArray;

@SuppressWarnings("serial")
public class SandymobilewebServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html");
		//resp.getWriter().println("Hello, world");
		PrintWriter out = resp.getWriter();
		 String str1, str2, str3, str4;
		Vector<String> v1 = new Vector<String>();
		Vector<String> v2 = new Vector<String>();
		Vector<String> v3 = new Vector<String>();
		Vector<String> v4 = new Vector<String>();
		JSONArray arrayObj=new JSONArray();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Details");
		List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
		int sizeofdatabase = results.size()+1;
		String name= req.getParameter("name");
		String email= req.getParameter("email");
		String pwd = req.getParameter("pwd");
		String num= req.getParameter("num");
		Entity feedbacks = new Entity("Details",sizeofdatabase);
		feedbacks.setProperty("name",name);
		feedbacks.setProperty("email",email);
		feedbacks.setProperty("pwd",pwd);
		feedbacks.setProperty("num",num);
		datastore.put(feedbacks);
		//q.getSortPredicates();
		
		q.addSort("name");
		PreparedQuery pq = datastore.prepare(q);
		for (Entity result : pq.asIterable()) {
			  str1 = (String) result.getProperty("name");
			  str2 = (String) result.getProperty("email");
			  str3 = (String) result.getProperty("pwd");
			  str4 = (String) result.getProperty("num");
			  
			  v1.add(str1);
			  v2.add(str2);
			   v3.add(str3);
			    v4.add(str4);
			  
		}
		out.println("<html>");
		out.println("<head>");
		out.println("<title> Resutls</title>");
		out.println("</head>");
		out.println("<body background = images/_original.jpg >");
		out.println("<a href= index.html><img type = button src = home.png ></a>");
		for(int i=0;i<v1.size();i++)
		{
			out.println("<p>Name:"+v1.elementAt(i)+"     ");
			out.println("Email:"+v2.elementAt(i)+"     ");
			out.println("PassWord:"+v3.elementAt(i)+"     ");
			out.println("Contact Number:"+v4.elementAt(i)+"     "+"</p></br>");	
		}
		out.println("</body>");
		out.println("</html>");
		out.close();
	
	}
}
	

